package action;


import com.opensymphony.xwork2.ModelDriven;
import org.apache.commons.io.FileUtils;
import entity.User;
import service.UserService;
import service.impl.UserServiceImpl;

import java.io.File;
import java.net.URL;

public class MoterAction implements ModelDriven<User> {
    // 创建头像目录
    static {
        synchronized (MoterAction.class) {
            File file = new File(System.getProperty("user.dir") + "\\img\\");
            if (!file.exists()) {
                file.mkdirs();
            }

        }
    }
    private User user;
    private static final UserService userService = new UserServiceImpl();
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    //头像文件
    private File avatar;
    //头像文件名
    private String avatarFileName;
    //头像文件格式
    private String avatarContentType;

    public File getAvatar() {
        return avatar;
    }

    public void setAvatar(File avatar) {
        this.avatar = avatar;
    }

    public String getAvatarFileName() {
        return avatarFileName;
    }

    public void setAvatarFileName(String avatarFileName) {
        this.avatarFileName = avatarFileName;
    }

    public String getAvatarContentType() {
        return avatarContentType;
    }

    public void setAvatarContentType(String avatarContentType) {
        this.avatarContentType = avatarContentType;
    }

    private final String AVATAR_DEFAULT_PATH;

    // 为了获取默认头像地址
    {
        URL resource = this.getClass().getClassLoader().getResource("/");
        assert resource != null;
        AVATAR_DEFAULT_PATH = resource.getPath().substring(1) + "/static/default.png";
    }

    public String login() throws Exception {
        User byName = userService.getByUsername(user.getUsername());
        if (byName!=null&&byName.getPassword().equals(user.getPassword())) {
            user.setName(byName.getName());
            user.setId(byName.getId());
            avatarHandler();
            return "success";
        }
        return "fail";
    }

    /**
     * 处理用户头像上传问题
     *
     * @throws Exception
     */
    private void avatarHandler() throws Exception {
        String path = System.getProperty("user.dir") + "\\img\\" + user.getName() + "_";
        File src = null;
        if (avatar == null) {
            //            默认头像
            path += "default.png";
            src = new File(AVATAR_DEFAULT_PATH);
            if (!src.exists()) {
                return;
            }
        } else {
            //            用户名_avatar.xx
            path += "avatar." + avatarFileName.split("\\.")[1];
            src = avatar;
        }
        File destiny = new File(path);
        if (!destiny.exists()) {
            destiny.createNewFile();
        }
        // 文件拷贝
        FileUtils.copyFile(src, destiny);
        // 暂时不提供头像显示，只提供地址
        user.setAvatarPath(destiny.getAbsolutePath());

    }

    @Override
    public User getModel() {
        synchronized (MoterAction.class){
            if(user==null){
                user = new User();
            }
        }
        return user;
    }
}
